package com.onlyphoto.onlymall.mvp.view.activity

import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.request.RefundBean
import com.onlyphoto.onlymall.bean.response.RefundInfoBean
import com.onlyphoto.onlymall.bean.response.RefundReasonBean
import com.onlyphoto.onlymall.mvp.contract.CRefund
import com.onlyphoto.onlymall.mvp.presenter.PRefundImpl
import com.onlyphoto.onlymall.utils.ToastUtil
import com.onlyphoto.onlymall.widget.OptionPopupView
import kotlinx.android.synthetic.main.activity_refund.*


class RefundActivity : BaseActivity<PRefundImpl>(), CRefund.IVRefund, View.OnClickListener {

    var orderId = ""
    var price = 0.00

    var refundType = 0
    var reasonType = 0
    var popupView: OptionPopupView? = null
    override fun retry() {}

    override fun init() {
        refundType = intent.getIntExtra("refundType", 0)
        orderId = intent.getStringExtra("orderId")!!
        price = intent.getDoubleExtra("orderPrice", 0.00)

        if (refundType == 3) {
            rb_1.isEnabled = false
            rb_2.isEnabled = false
            reason.isEnabled = false
            total_price.isEnabled = false
            description.isEnabled = false
            refund.visibility = View.GONE
        }
    }

    override fun initListener() {
        //点击事件初始化
        reason.setOnClickListener(this)
        refund.setOnClickListener(this)
        back.setOnClickListener(this)

        total_price.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                if (s.toString().toDouble() > price) {
                    total_price.setText(price.toString())
                    total_price.setSelection(price.toString().length)
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.refund -> {
                val bean = RefundBean()
                bean.order_id = orderId
                if(rb_1.isChecked) {
                    bean.refund_type = 2
                } else {
                    bean.refund_type = 1
                }
                bean.put_type = refundType
                bean.reason_type = reasonType
                bean.refund_price = price
                mPresenter?.refund(bean)
            }
            R.id.reason -> showPopup()
            R.id.back -> finish()
        }
    }

    private fun showPopup(){
        val lp = window.attributes
        lp.alpha = 0.38f
        window.attributes = lp
        popupView?.showAtLocation(ll_refund, Gravity.BOTTOM, 0, 0)
    }

    override fun refundSuccess() {
        ToastUtil.showToast("提交成功")
        finish()
    }

    override fun getRefundInfoSuccess(bean: RefundInfoBean) {
        refund.isEnabled = true
        if (bean.refund_type == 2) {
            rb_1.isChecked = true
        } else {
            rb_2.isChecked = true
        }

        reason.text = bean.reason_type_cn
        total_price.setText(bean.refund_price.toString())
        total_price.setSelection(bean.refund_price.toString().length)
        description.setText(bean.reason_return)

    }

    override fun showMsg(msg: String?) {
        ToastUtil.showToast(msg)
    }

    override fun getRefundReasonSuccess(list: ArrayList<RefundReasonBean>){
        popupView = OptionPopupView(this, list)
        popupView?.setFinishedListener { id, name ->
            reasonType = id
            reason.text = name
            refund.isEnabled = true
        }
    }

    override fun loadData() {
        //加载数据
        mPresenter?.getRefundReason()
        if (refundType == 2 || refundType == 3) {
            mPresenter?.getRefundInfo(3, orderId)
        }
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    public override fun initCustomerStatusManager() {

    }

    override fun setContentViewId(): Int {
        return R.layout.activity_refund
    }

    override fun createPresenter() {
        mPresenter = PRefundImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

}
